﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentValidation;
using Microhard.WarehouseInsp.Buz.Models;
using Microhard.WarehouseInsp.Buz.Msg;
using Microhard.WarehouseInsp.Buz.Interfaces;
using Microhard.Core;
using FluentValidation.Results;

namespace Microhard.WarehouseInsp.Buz.Validator
{
    public class WarehouseModelValidator : AbstractValidator<WarehouseModel>
    {
        public WarehouseModelValidator()
        {
            RuleSet("Insert", () =>
            {
                RuleFor(u => u.Name).NotEmpty().WithMessage(WarehouseMsg.NeedName).When(r => r.OperationStatus == Core.DBUtility.OperationStatus.Add);
                RuleFor(u => u.Name).Length(1, 20).WithMessage(WarehouseMsg.MaxNameLength).When(r => r.OperationStatus == Core.DBUtility.OperationStatus.Add);

                RuleFor(u => u.WarehouseMngID).NotEmpty().WithMessage(WarehouseMsg.NeedMngID).When(r => r.OperationStatus == Core.DBUtility.OperationStatus.Add);

                RuleFor(u => u.WorkCondition).NotEmpty().WithMessage(WarehouseMsg.NeedWorkCondition).When(r => r.OperationStatus == Core.DBUtility.OperationStatus.Add);
                RuleFor(u => u.WorkCondition).Length(1, 250).WithMessage(WarehouseMsg.MaxWorkCondition).When(r => r.OperationStatus == Core.DBUtility.OperationStatus.Add);

                RuleFor(u => u.LiveCondition).NotEmpty().WithMessage(WarehouseMsg.NeedLiveCondition).When(r => r.OperationStatus == Core.DBUtility.OperationStatus.Add);
                RuleFor(u => u.LiveCondition).Length(1, 250).WithMessage(WarehouseMsg.MaxLiveCondition).When(r => r.OperationStatus == Core.DBUtility.OperationStatus.Add);

                RuleFor(u => u.EatCondition).NotEmpty().WithMessage(WarehouseMsg.NeedEatCondition).When(r => r.OperationStatus == Core.DBUtility.OperationStatus.Add);
                RuleFor(u => u.EatCondition).Length(1, 250).WithMessage(WarehouseMsg.MaxEatCondition).When(r => r.OperationStatus == Core.DBUtility.OperationStatus.Add);
                Custom(u =>
                {

                    IWarehouse iclt = IoC.Resolve<IWarehouse>();
                    if (iclt.IsNameDuplicate(u))
                    {
                        return new ValidationFailure("Name", WarehouseMsg.DuplicateName);
                    }
                    if (u.StartTime > u.EndTime)
                    {
                        return new ValidationFailure("StartTime", WarehouseMsg.StartNotGreatThanEnd);
                    }
                    return null;
                });
            });

            RuleSet("Update", () =>
            {
                RuleFor(u => u.WarehouseMngID).NotEmpty().WithMessage(WarehouseMsg.NeedMngID);


                RuleFor(u => u.WorkCondition).NotEmpty().WithMessage(WarehouseMsg.NeedWorkCondition);
                RuleFor(u => u.WorkCondition).Length(1, 250).WithMessage(WarehouseMsg.MaxWorkCondition);

                RuleFor(u => u.LiveCondition).NotEmpty().WithMessage(WarehouseMsg.NeedLiveCondition);
                RuleFor(u => u.LiveCondition).Length(1, 250).WithMessage(WarehouseMsg.MaxLiveCondition);

                RuleFor(u => u.EatCondition).NotEmpty().WithMessage(WarehouseMsg.NeedEatCondition);
                RuleFor(u => u.EatCondition).Length(1, 250).WithMessage(WarehouseMsg.MaxEatCondition);

                Custom(u =>
                {
                    if (u.StartTime > u.EndTime)
                    {
                        return new ValidationFailure("StartTime", WarehouseMsg.StartNotGreatThanEnd);
                    }
                    return null;
                });
            });
        }
    }
}
